{% import 'macros/form.html' as form %}

{% set placeholder = placeholder if placeholder else _('Search datasets...') %}
{% set sorting = sorting if sorting else [(_('Name Ascending'), 'name asc'), (_('Name Descending'), 'name desc')] %}
{% set search_class = search_class if search_class else 'search-giant' %}
{% set no_bottom_border = no_bottom_border if no_bottom_border else false %}
{% set form_id = form_id if form_id else false %}

<form {% if form_id %}id="{{ form_id }}" {% endif %}class="search-form{% if no_bottom_border %} no-bottom-border{% endif %}" method="get" data-module="select-switch">

  {% block search_input %}
    <div class="input-group search-input-group">
      <input aria-label="{% block header_site_search_label %}{{ placeholder }}{% endblock %}" id="field-giant-search" type="text" class="form-control input-lg" name="q" value="{{ query }}" autocomplete="off" placeholder="{{ placeholder }}">
      {% block search_input_button %}
      <span class="input-group-btn">
        <button class="btn btn-default btn-lg" type="submit" value="search">
          <i class="fa fa-search"></i>
        </button>
      </span>
      {% endblock %}
    </div>
  {% endblock %}

  {% block search_search_fields %}
    {% if fields -%}
      <span>{{ form.hidden_from_list(fields=fields) }}</span>
    {%- endif %}
  {% endblock %}

  {% block search_sortby %}
    {% if sorting %}
      <div class="form-select form-group control-order-by">
        <label for="field-order-by">{{ _('Order by') }}</label>
        <select id="field-order-by" name="sort" class="form-control">
          {% for label, value in sorting %}
            {% if label and value %}
              <option value="{{ value }}"{% if sorting_selected == value %} selected="selected"{% endif %}>{{ label }}</option>
            {% endif %}
          {% endfor %}
        </select>
        {% block search_sortby_button %}
        <button class="btn btn-default js-hide" type="submit">{{ _('Go') }}</button>
        {% endblock %}
      </div>
    {% endif %}
  {% endblock %}

  {% block search_title %}
    {% if not error %}
      <h2>{% snippet 'snippets/search_result_text.html', query=query, count=count, type=type %}</h2>
    {% else %}
      <h2>Error</h2>
    {% endif %}
  {% endblock %}

  {% block search_facets %}
    {% if facets %}
      <p class="filter-list">
        {% for field in facets.fields %}
          {% set search_facets_items = facets.search.get(field)['items'] %}
          <span class="facet">{{ facets.titles.get(field) }}:</span>
          {% for value in facets.fields[field] %}
            <span class="filtered pill">
              {%- if facets.translated_fields and (field,value) in facets.translated_fields -%}
                {{ facets.translated_fields[(field,value)] }}
              {%- else -%}
                {{ h.list_dict_filter(search_facets_items, 'name', 'display_name', value) }}
              {%- endif %}
              <a href="{{ facets.remove_field(field, value) }}" class="remove" title="{{ _('Remove') }}"><i class="fa fa-times"></i></a>
            </span>
          {% endfor %}
        {% endfor %}
      </p>
      <a class="show-filters btn btn-default">{{ _('Filter Results') }}</a>
    {% endif %}
  {% endblock %}

</form>

{% if show_empty and count == 0 and not error %}
  {% trans %}
    <p class="extra">Please try another search.</p>
  {% endtrans %}
{% endif %}

{% if error %}
  {% trans %}
    <p id="search-error"><strong>There was an error while searching.</strong> Please try again.</p>
  {% endtrans %}
{% endif %}
